package cn.kgc.permanager01.controller;

import cn.kgc.permanager01.pojo.Person;
import cn.kgc.permanager01.pojo.ResultEntity;
import cn.kgc.permanager01.service.PersonService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create
 */
@CrossOrigin("*")
@RestController
public class PersonController {
    @Resource
    PersonService personService;

    @RequestMapping("/selAll")
    @ApiOperation("动态查询并分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "name",value = "姓名"),
            @ApiImplicitParam(name = "gender",value = "性别"),
            @ApiImplicitParam(name = "age",value = "年龄"),
            @ApiImplicitParam(name = "rank",value = "等级"),
            @ApiImplicitParam(name = "department",value = "部门"),
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false),
            @ApiImplicitParam(name = "pageSize",value = "页面行数",required = false)
    })
    public ResultEntity<PageInfo<Person>> select(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "4") Integer pageSize
    ){
        PageInfo<Person> pageInfo=personService.select(pageNum,pageSize);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @RequestMapping("/save")
    @ApiOperation("删除或增加")
    @ApiImplicitParam(name = "id",value = "id")
    public ResultEntity<String> save(@RequestBody Person person){
        try {
            Integer integer=0;
            if (person.getId() == null) {
                //添加
                integer = personService.add(person);
            } else {
                //删除
                integer = personService.update(person);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex) {
            return ResultEntity.failed(ex.getMessage());
        }
    }

    //根据id查询
    @RequestMapping("/getById")
    @ApiOperation("根据id查询")
    @ApiImplicitParam(name = "id",value = "id",required = true)
    public ResultEntity<Person> getPersonById(Integer id){
        Person person=personService.selectById(id);
        return ResultEntity.successWithoutData(person);
    }

    //根据id删除
    @RequestMapping("/delById")
    @ApiOperation("删除")
    @ApiImplicitParam(name = "id",value = "id")
    public ResultEntity<String> delPersonById(Integer id){
        try{
            Integer integer=personService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }
}
